<template>
	<view class="full box relative vip-wrapper " :style="{'--height':statusBarHeightValue}">
		<view class="page-full-mask">
			<image :src="sourceUrl + '/user-mask.png'" class="full-mask-img" mode="aspectFill"></image>
		</view>
		<view class="page-content">
			<view class="custom-nav-bar">
				<image @click="backHandler" src="../static/fanhui.png" mode="aspectFill" class="back-icon"></image>
			</view>
			<view class="userinfo-wrap box">
				<view class="avatar-wrap" @click="previewImage">
					<u-image :src="userInfo.avatar" width="112rpx" height="112rpx" shape="circle"></u-image>
				</view>
				<view class="vip-wrap"
					:style="[{'--level-progress':vipData.progress,'--level-color':vipData.levelColor}]">
					<view class="level-image">
						<image :src="vipData.dengji_icon_image" mode="widthFix" class="level-icon"></image>
						<view class="username">
							<text>{{ userInfo.nickname }}</text>
						</view>
					</view>
					<view class="level-progress">
						<view class="pregress-inner"></view>
					</view>
					<div class="level-desc">
						<text class="current-value">{{ vipData.current_li_cheng }}</text>
						<text>/</text>
						<text class="current-value">{{ vipData.next_li_cheng }}</text>
						<text class="update-text">升级VIP{{ vipData.nextLevel }}</text>
					</div>
				</view>
			</view>
			<view class="vip-level">
				<swiper class="full">
					<swiper-item v-for="(item,index) in list" :key="index">
						<view class="full vip-item">
							<view class="vip-top relative box">
								<image mode="aspectFill" class="full" :src="item.fengmian_image"></image>
								<view class="top-desc" v-if="item.id == 6 && isV6">
									<text>当前已是最高等级,可享最高专属优惠</text>
								</view>
								<view class="top-desc">
									<text>还需{{ item.needValue }}</text>
									<text>里程,</text>
									<text>可升级至</text>
									<text class="vip-text">VIP{{ item.nextLevel }}</text>
								</view>
							</view>
							<view class="vip-bottom box">
								<image mode="aspectFill" :src="item.quanyi_image" class="full"></image>
							</view>
						</view>
					</swiper-item>
				</swiper>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				list: [],
				isV6: false,
				vipData: {
					dengji_icon_image: "",
					currentLevel: 1,
					nextLevel: 1,
					hasNext: true,
					progress: '0%',
					fengmian_image: "",
					quanyi_image: "",
					current_li_cheng: "0",
					next_li_cheng: "0",
					levelColor: "#3a355f",
				}
			}
		},
		created() {
			this.getUserVip()
		},
		methods: {
			backHandler() {
				uni.navigateBack()
			},
			patchLevelList(data) {
				const {
					level_list,
					user_info
				} = data
				const {
					licheng,
					level
				} = user_info
				const list = level_list.filter(item => item.id * 1 >= level * 1)
				const len = list.length
				const result = list.map((item, index) => {
					const nextItem = list[index + 1]
					item.hasNext = !!nextItem
					item.needValue = item.licheng - licheng
					item.nextLevel = item.id
					return item
				})
				console.log(result, 'result');
				this.list = result
			},
			calcUserVipData(vipData) {
				function calcProgress(current, next) {
					return (current / next).toFixed(2) * 100 + '%'
				}
				const {
					user_info,
					level_list,
				} = vipData
				const {
					licheng,
					level
				} = user_info
				this.isV6 = level * 1 === 6
				const targetLevelIndex = level_list.findIndex(item => item.id * 1 === level * 1)
				if (targetLevelIndex < 0) return
				const currentLevelTarget = level_list[targetLevelIndex]
				const nextLevelTarget = level_list[targetLevelIndex + 1]
				const {
					dengji_icon_image,
					fengmian_image,
					quanyi_image
				} = currentLevelTarget
				const hasNext = !!nextLevelTarget
				const nextLevel = hasNext ? nextLevelTarget.id * 1 : ''
				return {
					dengji_icon_image,
					currentLevel: level,
					nextLevel,
					hasNext,
					progress: hasNext ? calcProgress(licheng, nextLevelTarget.licheng) : "100%",
					fengmian_image,
					quanyi_image,
					current_li_cheng: licheng,
					next_li_cheng: hasNext ? nextLevelTarget.licheng : '0',
					levelColor: "#3a355f"
				}
			},
			async getUserVip() {
				const resp = await this.$request({
					url: "/vip/index",
					method: "POST"
				})
				this.patchLevelList(resp.data)
				const result = this.calcUserVipData(resp.data)
				if (!result) return
				this.vipData = result
			},
		},
		computed: {
			...mapGetters(['userInfo', 'hasLogin', 'userIdentity']),
			statusBarHeightValue() {
				let height = uni.upx2px(88) + 'px'
				// #ifndef H5
				height = this.statusBarHeight_
				// #endif
				return height
			},
		},

	}
</script>

<style lang="scss" scoped>
	.page-content {
		z-index: 2;
		padding-top: var(--height);
		width: 100%;

		.userinfo-wrap {
			display: flex;
			align-items: center;
			padding: 4rpx 32rpx;
			box-sizing: border-box;
			width: 100%;

			.vip-wrap {
				padding-left: 8rpx;
				box-sizing: border-box;

				.level-progress {
					margin-top: 12rpx;
					width: 160rpx;
					height: 10rpx;
					box-sizing: border-box;
					position: relative;
					border-radius: 128rpx;
					overflow: hidden;
					background-color: #fff;
					margin-left: 8rpx;

					.pregress-inner {
						width: var(--level-progress);
						height: 100%;
						background-color: var(--level-color);
					}
				}

				.level-desc {
					display: flex;
					align-items: center;
					font-size: 22rpx;
					color: var(--level-color);
					margin-top: 16rpx;

					.update-text {
						margin-left: 12rpx;
						font-style: oblique;
						font-weight: bold;
					}

					.current-value {
						font-weight: bold;
						font-style: italic;
					}
				}

				.level-image {
					font-size: 0;
					display: flex;
					align-items: center;

					.username {
						height: 48rpx;
						font-size: 32rpx;
						font-weight: bold;
						color: rgba(0, 0, 0, 0.9);
						line-height: 48rpx;
						margin-left: 16rpx;
					}

					.level-icon {
						width: 122rpx;
						height: 46rpx;
					}
				}
			}

			.info-header-wrap {
				margin-left: 16rpx;
				padding: 8rpx 0;

				.user-id {
					margin-top: 8rpx;
					height: 40rpx;
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.4);
					line-height: 40rpx;
				}

				.top-wrap {
					display: flex;
					align-items: center;

					.username {
						height: 48rpx;
						font-size: 32rpx;
						font-weight: bold;
						color: rgba(0, 0, 0, 0.9);
						line-height: 48rpx;
						margin-right: 16rpx;
					}

					.edit-btn {
						display: flex;
						align-items: center;
						padding: 8rpx 14rpx;
						border-radius: 84rpx 84rpx 84rpx 84rpx;
						border: 1rpx solid rgba(0, 0, 0, 0.9);
						justify-content: center;

						.edit-icon {
							width: 24rpx;
							height: 24rpx;
						}

						.edit-title {
							font-size: 16rpx;
							color: rgba(0, 0, 0, 0.9);
							margin-left: 8rpx;
						}
					}
				}
			}

			.avatar-wrap {
				width: 112rpx;
				height: 112rpx;
			}
		}


		.custom-nav-bar {
			display: flex;
			align-items: center;
			padding: 0 32rpx;
			box-sizing: border-box;
			position: relative;
			height: 88rpx;

			.back-icon {
				width: 44rpx;
				height: 44rpx;
			}
		}

	}


	.page-full-mask {
		width: 100%;
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: -1;

		.full-mask-img {
			width: 100%;
			height: 100%;
		}
	}

	.vip-item {
		display: flex;
		flex-flow: column;

		.vip-top {
			width: 100%;
			height: 360rpx;
			flex-shrink: 0;

			.top-desc {
				position: absolute;
				left: 52rpx;
				bottom: 52rpx;
				font-size: 24rpx;
				display: flex;
				align-items: center;
				color: #3a355f;
				font-weight: bold;

				.vip-text {
					font-style: oblique;
				}
			}
		}

		.vip-bottom {
			flex: 1;
			position: relative;
			margin-top: 32rpx;
		}
	}

	.vip-level {
		width: 100%;
		height: 70vh;
		box-sizing: border-box;
		margin-top: 32rpx;
	}
</style>